package com.jimmy.medical.service.impl;

import com.jimmy.medical.entity.*;
import com.jimmy.medical.enums.OrderStatusEnum;
import com.jimmy.medical.enums.PictureUseTypeEnum;
import com.jimmy.medical.enums.YnEnum;
import com.jimmy.medical.repository.*;
import com.jimmy.medical.service.IDiagnoseFlowOrderService;
import com.jimmy.medical.util.CommonUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Qualifier("diagnoseFlowOrderService")
public class DiagnoseFlowOrderServiceImpl extends FlowOrderServiceImpl
        implements IDiagnoseFlowOrderService {

    @Override
    public Long saveAcceptInfo(Long flowOrderId, Long userId) {
        FlowOrder flowOrder = process(flowOrderId, userId, "接号", 1);
        return getCountDown(flowOrder);
    }

    @Override
    public Long saveNoticeInfo(PhoneCall call) {
        FlowOrder flowOrder = process(call.getOrderId(), call.getUserId(), "电话通知", 2);
        return getCountDown(flowOrder);
    }

    @Override
    public Long saveMessageFlow(Message message) {
        FlowOrder flowOrder = process(message.getFlowOrderId(), message.getUserId(), "短信告知", 3);
        return getCountDown(flowOrder);
    }

    @Override
    public Long saveTakeNoInfo(Long flowOrderId, Long userId) {
        FlowOrder flowOrder = process(flowOrderId, userId, "取号,整理客户资料", 4);
        return getCountDown(flowOrder);
    }

    @Override
    public Long saveAppearance(Long flowOrderId, Long userId, Long photoId) {
        FlowOrder flowOrder = process(flowOrderId, userId, "自检仪容", 5);

        UploadFile photo = uploadFileRepository.findOne(photoId);
        photo.setUseType(PictureUseTypeEnum.CHECK_APPEARANCE.getKey());
        photo.setRelatedKey(flowOrder.getId().toString());
        uploadFileRepository.save(photo);

        return getCountDown(flowOrder);
    }

    @Override
    public Long saveFeeProcess(Long flowOrderId, Long userId) {
        return saveFeeProcess(flowOrderId, userId, 6);
    }

    @Override
    public Long saveDiagnose(Long flowOrderId, Long userId, String photoIds) {
        FlowOrder flowOrder = process(flowOrderId, userId, "陪诊", 7);
        if(StringUtils.isNotBlank(photoIds)) {
            String[] idArray = photoIds.split(",");
            for (String photoId : idArray) {
                UploadFile photo = uploadFileRepository.findOne(Long.valueOf(photoId));
                photo.setRelatedKey(flowOrder.getOrder().getId().toString());
                photo.setUseType(PictureUseTypeEnum.DIAGNOSE.getKey());
                uploadFileRepository.save(photo);
            }
        }

        return getCountDown(flowOrder);
    }

    @Override
    public Long saveDiagnoseRecord(OrderExecutionRecord record, String exceptionData, String diagnoseData, String medicineData) {
        return saveExecutionRecord(record, exceptionData, diagnoseData, medicineData, 8, true);
    }

    @Override
    public Long saveDiagnoseRecord(OrderExecutionRecord record, String exceptionData, String diagnoseData, String medicineData, boolean processFlow) {
        return saveExecutionRecord(record, exceptionData, diagnoseData, medicineData, 0, false);
    }

    @Override
    public FlowOrder submitDiagnose(Long flowOrderId, Long userId) {
        FlowOrder flowOrder = process(flowOrderId, userId, "完成陪诊", 9);
        return getNextFlowOrder(flowOrder);
    }
}